package org.bbl.utils;

import org.bbl.db.domain.Bbl;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

/**
 * @author biniam.gebremichael
 * @since 4/18/13
 */
public class SendMail {

    public static String compose(Bbl bbl, String ip, String bookName, String correction, String comment) {
        StringBuilder s = new StringBuilder();
        s.append("http://www.ip-adress.com/ip_tracer/")
                .append(ip)
                .append(" <BR><BR>")
                .append(" \n\n")
                .append("<A HREF=\"http://www.geezexperience.com/edittigrignabible/edit.php?book=")
                .append(bbl.getBookId())
                .append("&chapter=")
                .append(bbl.getChapterId())
                .append("&ln=ti&verse=")
                .append(bbl.getStartVerse())
                .append("&correction=")
                .append(correction)
                .append("\"> ")
                .append(bookName)
                .append(" ")
                .append(bbl.getChapterId())
                .append(":")
                .append(bbl.getStartVerse())
                .append(" </A><BR><BR>")
                .append(correction)
                .append(" new <BR><BR>")
                .append(bbl.getVerse())
                .append(" old <BR><BR>")
                .append("comment:")
                .append(comment)
                .append("<BR><BR>")
                .append("change:")
                .append(isSame(bbl.getVerse(),correction));
        return s.toString();
    }

    public static String isSame(String a, String b) {
        if (b == null || a == null) {
            return "ONE NUULL";
        }
        if (a.equals(b)) {
            return "SAME";
        } else {
            return "DIFF";
        }
    }

    public static void send(String body) {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        Multipart mp = new MimeMultipart();
        MimeBodyPart htmlPart = new MimeBodyPart();
        Message msg = new MimeMessage(session);
        try {
            htmlPart.setContent(body, "text/html; charset=utf-8");
            mp.addBodyPart(htmlPart);

            msg.setFrom(new InternetAddress("info@geezexperience.com", "Geez"));
            msg.addRecipient(Message.RecipientType.TO,
                    new InternetAddress("biniamg@gmail.com", "biniamg"));
            msg.setSubject("[geez] change request");
            msg.setContent(mp);
            Transport.send(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
